package cn.ixinjiu.libraryMangement.controller.systemAdmin.borrow;

import cn.ixinjiu.libraryMangement.dao.IBorrowInfoDao;
import cn.ixinjiu.libraryMangement.dao.impl.BorrowInfoDaoImpl;
import cn.ixinjiu.libraryMangement.entity.BorrowInfo;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

/**
 * Created by XinChen on 2023-05-19
 *
 * @TODO 借阅信息修改
 */
@WebServlet("/borrowInfoUpd")
public class BorrowInfoUpdServlet extends HttpServlet {
    private IBorrowInfoDao iBorrowInfoDao = new BorrowInfoDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-Type", "text/html;charset=utf-8");
        // test
        System.out.println("--------------------------------------------------");
        System.out.println("/borrowInfoUpd");
        // 获取前端参数
        int id = Integer.valueOf(req.getParameter("id"));
        int userId = Integer.valueOf(req.getParameter("user_id"));
        int bookId = Integer.valueOf(req.getParameter("book_id"));
        Date borrowDate = Date.valueOf(req.getParameter("borrow_date"));
        Date returnDate = Date.valueOf(req.getParameter("return_date"));
        Date actualReturn = Date.valueOf(req.getParameter("actual_return"));
        int isReturned = Integer.valueOf(req.getParameter("is_returned"));
        int isOverdue = Integer.valueOf(req.getParameter("is_overdue"));
        int overdueDays = Integer.valueOf(req.getParameter("overdue_days"));
        BorrowInfo borrowInfo = new BorrowInfo(id, userId, bookId, borrowDate, returnDate, actualReturn, isReturned, isOverdue, overdueDays);
        // 调用修改方法
        int result = iBorrowInfoDao.borrowUpd(borrowInfo);
        // 返回数据
        JSONObject json = new JSONObject();
        if (result == 1) {
            json.put("code", "0");
            json.put("msg", "修改成功");
        } else {
            json.put("code", "1");
            json.put("msg", "修改失败");
        }
        // 输出到页面
        PrintWriter writer = resp.getWriter();
        writer.write(json.toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
